package main

import (
	"fmt"

	"github.com/btcsuite/btcd/btcec/v2"
	"github.com/btcsuite/btcd/btcec/v2/schnorr"
	"github.com/btcsuite/btcd/btcutil"
	"github.com/btcsuite/btcd/chaincfg"
	"github.com/btcsuite/btcd/txscript"
)

func main() {
	// 生成私钥（或从现有私钥导入）
	privateKey, _ := btcec.NewPrivateKey()
	privateKeyBytes := privateKey.Serialize()

	// 生成公钥及其哈希
	pubKey := privateKey.PubKey()
	pubKeyBytes := pubKey.SerializeCompressed()
	pubKeyHash := btcutil.Hash160(pubKeyBytes)

	// 主网参数
	params := &chaincfg.MainNetParams

	// 1. Legacy (P2PKH)
	legacyAddr, _ := btcutil.NewAddressPubKeyHash(pubKeyHash, params)

	// 2. Nested SegWit (P2SH)
	witnessAddr, _ := btcutil.NewAddressWitnessPubKeyHash(pubKeyHash, params)
	script, _ := txscript.PayToAddrScript(witnessAddr)
	p2shAddr, _ := btcutil.NewAddressScriptHash(script, params)

	// 3. Native SegWit (Bech32)
	bech32Addr, _ := btcutil.NewAddressWitnessPubKeyHash(pubKeyHash, params)

	// 4. Taproot (P2TR)
	// 生成 Taproot 地址
	taprootInternalKey := pubKey
	taprootKey := txscript.ComputeTaprootKeyNoScript(taprootInternalKey)
	taprootKeyBytes := schnorr.SerializePubKey(taprootKey) // 关键修正
	taprootAddr, _ := btcutil.NewAddressTaproot(taprootKeyBytes, &chaincfg.MainNetParams)

	// 输出结果
	fmt.Printf("私钥 : %x\n", privateKey)
	fmt.Printf("私钥 (Hex): %x\n", privateKeyBytes)
	fmt.Printf("Legacy (P2PKH): %s\n", legacyAddr.EncodeAddress())
	fmt.Printf("Nested SegWit (P2SH): %s\n", p2shAddr.EncodeAddress())
	fmt.Printf("Native SegWit (Bech32): %s\n", bech32Addr.EncodeAddress())
	fmt.Printf("Taproot (P2TR): %s\n", taprootAddr.EncodeAddress())
}
